package com.sufun.fetches;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.ab.util.AbStrUtil;
import com.google.gson.Gson;
import com.sufun.base.SFBaseApplication;
import com.sufun.base.SFConfig;
import com.sufun.models.DataResultModel;

public class SFBaseFetch<T> implements IFetch<T>{
	
	String url =SFConfig.webBaseApi;
	public IFetch<T>  mFetchAnsyCallback;
	/**
	 * 当前存储的数据
	 */
	public List<T> allDatas=new ArrayList<T>();
	/**
	 * 当前的面码
	 */
	private int current_page=0;
	/**
	 * 请求参数
	 */
	AbRequestParams params=new AbRequestParams();
	
	/**
	 * 需要返回的数据类型
	 */
	public Type type;
	
	public SFBaseFetch(AbRequestParams params,String webApi)
	{
		this.params=params;
		url=SFConfig.webBaseApi+webApi;
	}
	/**
	 * 退回至原来的页码
	 */
	void subPageNum()
	{
		if (current_page>0) {
			current_page=current_page-1;
		}
		
	}
	@Override
	public void OnLoadMore() {
		// TODO Auto-generated method stub
		//current_page++;
		if (mFetchAnsyCallback!=null) {
			mFetchAnsyCallback.OnLoadMore();
		}
		doPost();
	}
    /**
     * post提交参数
     */
	public void doPost()
	{
		// 绑定参数
		params.put("PageNum", current_page+"");
        SFBaseApplication.shareSFApplication().mAbHttpUtil.post(url,params, new AbStringHttpResponseListener() {
        	
        	// 获取数据成功会调用这里
        	@Override
        	public void onSuccess(int statusCode, String content) {
        		
				List<T> mdata = new ArrayList<T>();
				if (!AbStrUtil.isEmpty(content)) {
					Gson gson = new Gson();
					try {
						mdata = gson.fromJson(content, type);
						if (mdata.size() > 0) {
							current_page++;
							allDatas.addAll(mdata);
						}
					} catch (Exception ex) {
						mdata = new ArrayList<T>();
						subPageNum();
					}
				}			
        		OnSuccess(allDatas);
            };
            
            // 开始执行前
            @Override
			public void onStart() {
            	OnPrepare();
			}
            
            // 失败，调用
            @Override
			public void onFailure(int statusCode, String content,
					Throwable error) {
            	DataResultModel re=new DataResultModel();
            	re.state_code=statusCode;
            	re.content=content;
            	OnFail(re);
            	subPageNum();
			}

			// 完成后调用，失败，成功
            @Override
            public void onFinish() { 
            	SFBaseFetch.this.OnFinish();
            };
            
        });
	}
	
	/**
	 * get 取数据	
	 */
	public void doGet()
	{
		// 绑定参数
		params.put("PageNum", current_page+"");
        SFBaseApplication.shareSFApplication().mAbHttpUtil.get(url,params, new AbStringHttpResponseListener() {
        	
        	// 获取数据成功会调用这里
        	@Override
        	public void onSuccess(int statusCode, String content) {
        		
				List<T> mdata = new ArrayList<T>();
				if (!AbStrUtil.isEmpty(content)) {
					Gson gson = new Gson();
					try {
						mdata = gson.fromJson(content, type);
						if (mdata.size() > 0) {
							current_page++;
							allDatas.addAll(mdata);
						}
					} catch (Exception ex) {
						mdata = new ArrayList<T>();
						subPageNum();
					}
				}			
        		OnSuccess(allDatas);
            };
            
            // 开始执行前
            @Override
			public void onStart() {
            	OnPrepare();
			}
            
            // 失败，调用
            @Override
			public void onFailure(int statusCode, String content,
					Throwable error) {
            	DataResultModel re=new DataResultModel();
            	re.state_code=statusCode;
            	re.content=content;
            	OnFail(re);
            	subPageNum();
			}
			// 完成后调用，失败，成功
            @Override
            public void onFinish() { 
            	SFBaseFetch.this.OnFinish();
            };
            
        });
	}
	@Override
	public void OnPrepare() {
		// TODO Auto-generated method stub
		if (mFetchAnsyCallback!=null) {
			mFetchAnsyCallback.OnPrepare();
		}
	}

	@Override
	public void OnLoading(int data) {
		// TODO Auto-generated method stub
		if (mFetchAnsyCallback!=null) {
			mFetchAnsyCallback.OnLoading(data);
		}
	}

	@Override
	public void OnSuccess(List<T> datas) {
		// TODO Auto-generated method stub
		if (mFetchAnsyCallback!=null) {
			mFetchAnsyCallback.OnSuccess(datas);
		}
	}

	@Override
	public void OnFail(DataResultModel result) {
		// TODO Auto-generated method stub
		if (mFetchAnsyCallback!=null) {
			mFetchAnsyCallback.OnFail(result);
		}
	}

	@Override
	public void OnReFresh() {
		// TODO Auto-generated method stub
		//current_page++;
		if (mFetchAnsyCallback!=null) {
			mFetchAnsyCallback.OnReFresh();
		}
		doPost();
	}

	@Override
	public void OnFinish() {
		// TODO Auto-generated method stub
		if (mFetchAnsyCallback!=null) {
			mFetchAnsyCallback.OnFinish();
		}
	}

}
